﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS_DataSync
{
    /// <summary>
    /// 时间戳操作对象
    /// </summary>
    public static class TimeStampOper
    {
        /// <summary>
        /// 将数据转换为时间戳字符
        /// </summary>
        /// <param name="arValue"></param>
        /// <returns></returns>
        public static string ToTimeStamp(byte[] arValue)
        {
            return arValue.Aggregate("0x", (current, n) => current + n.ToString("X2"));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s">包含 TTimestamp 的字符串: [0x]xxxxxxxxxxxxxxxx</param>
        public static byte[] TimestampBytes(string s)
        {
            var bytes = new byte[8];
            if (s.Length == 18) // 长度为18时，带0x
                s = s.Substring(2);
            bytes[0] = Convert.ToByte(s.Substring(0, 2), 16);
            bytes[1] = Convert.ToByte(s.Substring(2, 2), 16);
            bytes[2] = Convert.ToByte(s.Substring(4, 2), 16);
            bytes[3] = Convert.ToByte(s.Substring(6, 2), 16);
            bytes[4] = Convert.ToByte(s.Substring(8, 2), 16);
            bytes[5] = Convert.ToByte(s.Substring(10, 2), 16);
            bytes[6] = Convert.ToByte(s.Substring(12, 2), 16);
            bytes[7] = Convert.ToByte(s.Substring(14, 2), 16);
            return bytes;
        }
        /// <summary>
        /// >运算符重载
        /// </summary>
        /// <param name="bytesA"></param>
        /// <param name="bytesB"></param>
        /// <returns></returns>
        public static bool CompareABigBBytes(byte[] bytesA, byte[] bytesB)
        {
            if (bytesB == null)
                return true;
            if (bytesA == null)
                return false;
            if (bytesA[0] > bytesB[0]) return true;
            else if (bytesA[0] < bytesB[0]) return false;
            if (bytesA[1] > bytesB[1]) return true;
            else if (bytesA[1] < bytesB[1]) return false;
            if (bytesA[2] > bytesB[2]) return true;
            else if (bytesA[2] < bytesB[2]) return false;
            if (bytesA[3] > bytesB[3]) return true;
            else if (bytesA[3] < bytesB[3]) return false;
            if (bytesA[4] > bytesB[4]) return true;
            else if (bytesA[4] < bytesB[4]) return false;
            if (bytesA[5] > bytesB[5]) return true;
            else if (bytesA[5] < bytesB[5]) return false;
            if (bytesA[6] > bytesB[6]) return true;
            else if (bytesA[6] < bytesB[6]) return false;
            if (bytesA[7] > bytesB[7]) return true;
            else if (bytesA[7] < bytesB[7]) return false;
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static byte[] GetTimestampByTimeStampBytes(object timeBytes)
        {
            if (timeBytes != null)
                //ImportDataLog.WriteLog(LogType.Warning, timeBytes.ToString());
                LogUtils.LogWarn("TimeStampOper", Developer.SysDefault, timeBytes.ToString());
            return timeBytes == null ? new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 } : TimestampBytes(timeBytes.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionName"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string GetTimestampByTimeStampString(object timeBytes)
        {
            return timeBytes == null ? ToTimeStamp(new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 }) : timeBytes.ToString();
        }
    }
}
